package com.hongxun.budget.ui.view.ys;

import android.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;

import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;
import com.hongxun.budget.R;
import com.hongxun.budget.ui.adapter.SpinnerAdapter;
import com.hongxun.budget.ui.fragment.ys.PointBasicCheckFragment;
import com.hongxun.budget.ui.view.BaseView;
import com.hongxun.budget.utils.DBUtils;
import com.hongxun.budget.utils.GreenDaoManager;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.budget.utils.T;
import com.hongxun.greenentity.tk.BMaterialEntity;
import com.hongxun.greenentity.tk.DPointEntity;
import com.hongxun.greenentity.ys.EPointEntity;

import java.util.Arrays;

/**
 * Created by admin on 2018/8/27.
 */

public class YsZsbyqView extends BaseView {
    SpinnerAdapter<BMaterialEntity> adapter3;
    SpinnerAdapter<String> adapter5;
    Spinner spinner3, spinner5;
    EditText span;
    EditText jiaodu;

    public YsZsbyqView(Fragment fragment) {
        super(fragment);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.view_ys_zsbyq;
    }

    @Override
    protected void initView() {
        super.initView();
        setProcess();
        span = getView(R.id.span);
        jiaodu = getView(R.id.jiaodu);
        spinner3 = getView(R.id.spinner3);//导线型号
        spinner5 = getView(R.id.spinner5);//导线线数
        spinner3.setAdapter(adapter3 = new SpinnerAdapter<BMaterialEntity>(context, DBUtils.getYsMaterialEntities((String) SPUtils.get(context, "project_ys_guid", "")), spinner3));
        spinner5.setAdapter(adapter5 = new SpinnerAdapter<String>(context, Arrays.asList(context.getResources().getStringArray(R.array.daoxian)), spinner5));
    }

    @Override
    public void initDefault() {
        if (SPUtils.get(context, "process", "").toString().equals("新增") && DBUtils.getYsIsDraw(context)) {
            DBUtils.setYsDefaultDxType(context, spinner3);
            DBUtils.setYsDefaultDxHeight(context, spinner5);
        }
        EPointEntity d = GreenDaoManager.getInstance().getmDaoSession().getEPointEntityDao().load(SPUtils.get(context, "UUID", "").toString());
        if (d != null) updateInit(d);
    }

    double v = 0.0;

    @Override
    public void setSpan(String beforeCode) {
        if (beforeCode == null) {
            span.setText("0");
        } else {
            EPointEntity EPointEntity = GreenDaoManager.getInstance().getmDaoSession().getEPointEntityDao().load(beforeCode);
            LatLng before = new LatLng(EPointEntity.getPPInputlatitude(), EPointEntity.getPPInputlongitude());
            LatLng latLng = new LatLng(((PointBasicCheckFragment) fragment).getLat(), ((PointBasicCheckFragment) fragment).getLgt());
            v = (double) Math.round(AMapUtils.calculateLineDistance(before, latLng) * 100) / 100;
            span.setText(v + "");
        }
    }

    EPointEntity d;

    @Override
    public boolean save(EPointEntity d) {
        if (DBUtils.getYsIsDraw(context) && SPUtils.get(context, "process", "").equals("新增")) {//作图
            if (TextUtils.isEmpty(span.getText().toString())) {
                T.showLong(context, "档距不能为空");
                return false;
            }
            d.setPPInputspan((double) Math.round(Double.parseDouble(span.getText().toString()) * 100) / 100);//档距
            d.setPWGuid(adapter3.getSelectedItem().getGuid());//导线
            d.setPPLinenum(Integer.parseInt(adapter5.getSelectedItem()));//线数
            if (TextUtils.isEmpty(jiaodu.getText().toString())) {
                T.showLong(context, "角度不能为空");
                return false;
            }
            d.setPPAngle(Double.parseDouble(jiaodu.getText().toString()));//角度
            d.setPPAutospan(v);
        }
        this.d = d;
        return true;
    }

    @Override
    public void insertDB() {
        GreenDaoManager.getInstance().getmDaoSession().getEPointEntityDao().insertOrReplace(d);
        //作图并且有上联点为的时候
        if (DBUtils.getIsDraw(context) && !d.getPPWorktype().equals("拆除") && !TextUtils.isEmpty(d.getPPUpguid()) && d.getPPInputspan() != 0) {//如果要作图，那个插入数据库的时候会自动生成导线数据到
            BMaterialEntity b = adapter3.getSelectedItem();
            DBUtils.addYsWire(context, b, d.getPPInputspan() * d.getPPLinenum());
        }
    }

    @Override
    public void updateInit(Object o) {
        EPointEntity d = (EPointEntity) o;
        span.setText(d.getPPInputspan() + "");
        v = d.getPPAutospan() == null ? 0 : d.getPPAutospan();
        int i = 0;
        for (BMaterialEntity b : DBUtils.getMaterialEntities(d.getPGuid())) {
            if (b.getGuid().equals(d.getPWGuid())) {
                spinner3.setSelection(i);
                break;
            }
            i++;
        }
        i = 0;
        for (String s : context.getResources().getStringArray(R.array.daoxian)) {
            if (Integer.parseInt(s) == (d.getPPLinenum() == null ? 0 : d.getPPLinenum())) {
                spinner5.setSelection(i);
                break;
            }
            i++;
        }
        jiaodu.setText(d.getPPAngle() + "");
    }

    @Override
    public void setProcess() {
        if (DBUtils.getYsIsDraw(context) && SPUtils.get(context, "process", "").equals("新增"))//作图显示
            getView(R.id.draw_layout).setVisibility(View.VISIBLE);
        else
            getView(R.id.draw_layout).setVisibility(View.GONE);
    }
}
